<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Hethong {
    private $hostname;
    private $username; 
    private $password;
    private $dbname;
    private $filename;
    public function setHost($hostname){
            $this->hostname = $hostname;
        }
    public function getHost(){
        
        return $this->hostname;
        }
    public function setUser($username){
            $this->username = $username;
        }
    public function getUser(){
            return $this->username;
        }
    public function setPass($password){
            $this->password = $password;
        }
    public function getPass(){
            return $this->password;
        }
    public function setBDname($dbname){
            $this->dbname = $dbname;
        }
    public function getBDname(){
            return $this->dbname;
        }
    public function setFilename($filename){
            $this->filename = $filename;
        }
    public function getFilename(){
            return $this->filename;
        }
    function export_database() {
        ob_start();
            $command = "H:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=".$this->getHost()." --user=".$this->getUser()." ";
            if ($this->getPass()) 
            $command.= "--password=". $this->getPass() ." "; 
            $command.= $this->getBDname();
            system($command);
            $dump = ob_get_contents(); 
            ob_end_clean();

            // the database dump now exists in the $dump variable
            // saving it to the file
            $dumpfname ='../llkh/backup/'.$this->getBDname() . "_" . date("Y-m-d_H-i-s").".sql";
            $this->setFilename($dumpfname);
            $fp = fopen($dumpfname, "w"); 
            fputs($fp, $dump); 
            fclose($fp);
            
        //ob_end_flush();           
    }
    function import_database() {
            
            // Connect to MySQL server
            mysql_connect($this->getHost(), $this->getUser(), $this->getPass()) or die('Lỗi kết nối đến MySQL server: ' . mysql_error());    
            // Select database
            mysql_select_db($this->getBDname()) or die('Lỗi kết nối đến cơ sở dữ liệu: ' . mysql_error());
              mysql_query("SET NAMES 'UTF8'");
            // Temporary variable, used to store current query
            $templine = '';
            // Read in entire file
            $lines = file($this->getFilename());
            // Loop through each line
            foreach ($lines as $line)
            {
                // Skip it if it's a comment
                if (substr($line, 0, 2) == '--' || $line == '')
                    continue;

                // Add this line to the current segment
                $templine .= $line;
                // If it has a semicolon at the end, it's the end of the query
                if (substr(trim($line), -1, 1) == ';')
                {
                    // Perform the query
                    mysql_query($templine) or print(' Lỗi cơ sở dữ liệu phục hồi \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
                    // Reset temp variable to empty
                    $templine = '';
                }
            }
    }
}
?>
